package com.sunmouse.toollib.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.lang.reflect.Field;

/**
 * Created by Kvin on 2017/6/28.
 */
public class TabChoice extends RadioGroup {

    private LayoutParams lp;
    private String[] tabNames;
    private int[] tabIcons;
    private int itemBackground;

    public TabChoice(Context context) {
        super(context);

    }

    public TabChoice(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * return an instance
     */
    public void init(Context context, String[] tabNames, int[] icons, ColorStateList txtColor, int itemBackground, Mode mode) {
        if (tabNames == null || icons == null) {
            throw new NullPointerException("tabNames or TabIcons is null");
        }
        this.tabNames = tabNames;
        this.tabIcons = icons;

        setOrientation(HORIZONTAL);
        for (int i = 0; i < tabNames.length; i++) {
            RadioButton mRadio = new RadioButton(context);
            mRadio.setLayoutParams(newLayoutParams());
            setButton(mRadio);
            mRadio.setBackgroundResource(itemBackground);
            mRadio.setText(TextUtils.isEmpty(tabNames[i]) ? "" : tabNames[i]);
            mRadio.setTextColor(txtColor);
            mRadio.setGravity(Gravity.CENTER);
//            mRadio.setBackgroundResource(tabBackground);
            mRadio.setCompoundDrawablesWithIntrinsicBounds(0, tabIcons[i], 0, 0);
//            mRadio.setBackground(getResources().getDrawable(R.drawable.nav_tab_bg));
            addView(mRadio);
        }
    }

    public void init(Context context, String[] tabNames, int[] icons, ColorStateList txtColor, int itemBackground) {
        this.init(context, tabNames, icons, txtColor, itemBackground);
    }

    /**
     * set Button drawable
     */
    private void setButton(RadioButton mRadio) {
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT_WATCH) {
            try {
                Field mField = mRadio.getClass().getSuperclass().getDeclaredField("mButtonDrawable");
                mField.setAccessible(true);
                mField.set(mRadio, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            mRadio.setButtonDrawable(null);
        }

    }

    /**
     * get layout param
     */
    private ViewGroup.LayoutParams newLayoutParams() {
        if (lp == null) {
            lp = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
            lp.weight = 1;
        }
        return lp;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    public enum Mode {
        NORMAL(0),

        MIDDLE(1);//you can layout other control in middle

        int value;

        Mode(int value) {
            this.value = value;
        }
    }
}
